package com.controller;

import com.dao.AnalyDao;
import com.mode.Analy;
import com.utils.IssueDto;
import com.utils.Issues;
import com.utils.SonarIssue;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: sksun2
 * @create: 2023-03-15
 */
@RestController
public class HelloController {

    @Autowired
    private AnalyDao analyDao;

//    @Autowired
//    private Environment environment;

    @Autowired
    private ConfigurableEnvironment environment;

    @PostConstruct
    public void init(){

        MutablePropertySources propertySources = environment.getPropertySources();
        for (PropertySource<?> propertySource : propertySources) {
            System.out.println(propertySource.getName());
        }



//        String[] activeProfiles = environment.getActiveProfiles();
//        for (String activeProfile : activeProfiles) {
//            System.out.println("======"+activeProfile);
//        }
    }

    private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(8, 16, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue(1000000),new ThreadPoolExecutor.DiscardPolicy());

    @org.junit.Test
    public void test(){

        Optional<Analy> byId = analyDao.findById(1L);
        exec(byId.get());

    }


    @RequestMapping("hello")
    public String te(@RequestParam("id") long id,@RequestParam("endId")Long endId){
        List<Analy> analyList = analyDao.findByIdLimit(id,endId);
        for (Analy analy : analyList) {
            System.out.println("当前id："+analy.getId());
//            exec(analy);
            final T t = new T();
            t.setAnaly(analy);

            threadPoolExecutor.submit(t);
        }
        return "hello";

    }

    @GetMapping("hello1")
    public String te(HttpServletRequest request){
        return "hello";
    }

    @org.junit.Test
    public void te1(){
        List<Analy> analyList = analyDao.findByIdLimitV2(10001L);
        for (Analy analy : analyList) {
            System.out.println("当前id："+analy.getId());
            exec(analy);
//            final T t = new T();
//            t.setAnaly(analy);
//
//            threadPoolExecutor.submit(t);
        }

    }


    private void exec(Analy analy){
        // https://sonarqube.iflytek.com:9100/dashboard?id=QYPT-QYAPIJKFW
        String address = analy.getAddress();
        if(StringUtils.isEmpty(address) || Objects.equals(address,"-")){
            return;
        }
        String serverUrl = address.substring(0,34);
        String projectKey = address.substring(48);

        SonarIssue sonarIssue = new SonarIssue();

        IssueDto admin = null;
        int pageNumber = 1;

        Long blockerCountAll = 0L;
        Long CRITICALCountAll = 0L;
        Long MAJORCountAll = 0L;
        Long MINORCountAll = 0L;


        Long unblockerCountAll = 0L;
        Long unCRITICALCountAll = 0L;
        Long unMAJORCountAll = 0L;
        Long unMINORCountAll = 0L;


        Long jiejuezuduanAll = 0L;
        Long jiejueyanzhongAll = 0L;
        Long jiejuezhuyaoAll = 0L;
        Long jiejueciyaoAll = 0L;


        Long shanchuzuduanAll = 0L;
        Long shanchuyanzhongAll = 0L;
        Long shanchuzhuyaoAll = 0L;
        Long shanchuciyaoAll = 0L;


        Long wupanzuduanAll = 0L;
        Long wupanyanzhongAll = 0L;
        Long wupanzhuyaoAll = 0L;
        Long wupanciyaoAll = 0L;

        // 解决总数
        Long FIXED_ALL = 0L;
        // 误判总数
        Long FALSE_POSITIVE_ALL = 0L;
        // 不修复总数
        Long WONTFIX_ALL = 0L;

        Long REMOVED_ALL = 0L;

        do{
            admin = sonarIssue.RetrieviewIssue(serverUrl, "admin", "s6xrc%3yjKPr", projectKey,pageNumber,500);

            List<Issues> issues = admin.getIssues();
            if(CollectionUtils.isEmpty(issues)){
                break;
            }
            // 【未处理】
//            final List<Issues> collect = issues.stream().filter(i -> Objects.equals(i.getStatus(), "REOPENED")
//                    || Objects.equals(i.getStatus(), "CONFIRMED")
//                    || Objects.equals(i.getStatus(), "OPEN")
//            ).collect(Collectors.toList());
            // 不修复
            final List<Issues> collect = issues.stream().filter(i -> Objects.equals(i.getResolution(), "WONTFIX")
            ).collect(Collectors.toList());

            // 未处理-阻断  BLOCKER
            long blockerCount = collect.stream().filter(i -> Objects.equals(i.getSeverity(), "BLOCKER")).count();
            blockerCountAll = blockerCount + blockerCountAll;

            // 未处理-严重 CRITICAL
            long CRITICALCount = collect.stream().filter(i -> Objects.equals(i.getSeverity(), "CRITICAL")).count();
            CRITICALCountAll = CRITICALCount + CRITICALCountAll;

            // 未处理-主要  MAJOR
            long MAJORCount = collect.stream().filter(i -> Objects.equals(i.getSeverity(), "MAJOR")).count();
            MAJORCountAll = MAJORCount + MAJORCountAll;

            // 未处理-次要  MINOR
            long MINORCount = collect.stream().filter(i -> Objects.equals(i.getSeverity(), "MINOR")).count();
            MINORCountAll = MINORCount + MINORCountAll;

            // 未处理-提示  INFO

            /***********    不修复   ****************/

            // 【不修复】
            final List<Issues> UNRESOLVED = issues.stream().filter(i -> Objects.equals(i.getStatus(), "RESOLVED")
            ).collect(Collectors.toList());

            // WONTFIX
            final List<Issues> WONTFIX = issues.stream().filter(i -> Objects.equals(i.getResolution(), "WONTFIX")
            ).collect(Collectors.toList());
            WONTFIX_ALL = WONTFIX.size() + WONTFIX_ALL;

            // 不修复-阻断  BLOCKER
            long unblockerCount = UNRESOLVED.stream().filter(i -> Objects.equals(i.getSeverity(), "BLOCKER")).count();
            unblockerCountAll = unblockerCount + unblockerCountAll;

            // 不修复-严重 CRITICAL
            long unCRITICALCount = collect.stream().filter(i -> Objects.equals(i.getSeverity(), "CRITICAL")).count();
            unCRITICALCountAll = unCRITICALCount + unCRITICALCountAll;

            // 不修复-主要  MAJOR
            long unMAJORCount = collect.stream().filter(i -> Objects.equals(i.getSeverity(), "MAJOR")).count();
            unMAJORCountAll = unMAJORCount + unMAJORCountAll;

            // 不修复-次要  MINOR
            long unMINORCount = collect.stream().filter(i -> Objects.equals(i.getSeverity(), "MINOR")).count();
            unMINORCountAll = unMINORCount + unMINORCountAll;

            /***********    误判   ****************/
            final List<Issues> FALSE_POSITIVE = issues.stream().filter(i -> Objects.equals(i.getResolution(), "FALSE-POSITIVE")
            ).collect(Collectors.toList());
            FALSE_POSITIVE_ALL = FALSE_POSITIVE.size() + FALSE_POSITIVE_ALL;

            // 误判-阻断  BLOCKER
            long wupanzuduan = FALSE_POSITIVE.stream().filter(i -> Objects.equals(i.getSeverity(), "BLOCKER")).count();
            wupanzuduanAll = wupanzuduan + wupanzuduanAll;

            // 误判-严重 CRITICAL
            long wupanyanzhong = FALSE_POSITIVE.stream().filter(i -> Objects.equals(i.getSeverity(), "CRITICAL")).count();
            wupanyanzhongAll = wupanyanzhong + wupanyanzhongAll;

            // 误判-主要  MAJOR
            long wupanzhuyao = FALSE_POSITIVE.stream().filter(i -> Objects.equals(i.getSeverity(), "MAJOR")).count();
            wupanzhuyaoAll = wupanzhuyao + wupanzhuyaoAll;

            // 误判-次要  MINOR
            long wupanciyao = FALSE_POSITIVE.stream().filter(i -> Objects.equals(i.getSeverity(), "MINOR")).count();
            wupanciyaoAll = wupanciyao + wupanciyaoAll;



            /***       解决 FIXED     ***/
            List<Issues> FIXED = issues.stream().filter(i -> Objects.equals(i.getResolution(), "FIXED")
            ).collect(Collectors.toList());
            FIXED_ALL = FIXED.size() + FIXED_ALL;

            // 解决-阻断  BLOCKER
            long jiejuezuduan = FIXED.stream().filter(i -> Objects.equals(i.getSeverity(), "BLOCKER")).count();
            jiejuezuduanAll = jiejuezuduan + jiejuezuduanAll;

            // 解决-严重 CRITICAL
            long jiejueyanzhong = FIXED.stream().filter(i -> Objects.equals(i.getSeverity(), "CRITICAL")).count();
            jiejueyanzhongAll = jiejueyanzhong + jiejueyanzhongAll;

            // 解决-主要  MAJOR
            long jiejuezhuyao = FIXED.stream().filter(i -> Objects.equals(i.getSeverity(), "MAJOR")).count();
            jiejuezhuyaoAll = jiejuezhuyao + jiejuezhuyaoAll;

            // 解决-次要  MINOR
            long jiejeuciyao = FIXED.stream().filter(i -> Objects.equals(i.getSeverity(), "MINOR")).count();
            jiejueciyaoAll = jiejeuciyao + jiejueciyaoAll;


//            /***       解决 FIXED     ***/
//            List<Issues> FIXED = issues.stream().filter(i -> Objects.equals(i.getResolution(), "FIXED")
//            ).collect(Collectors.toList());
//            FIXED_ALL = FIXED.size() + FIXED_ALL;
//
//            // 解决-阻断  BLOCKER
//            long jiejuezuduan = FIXED.stream().filter(i -> Objects.equals(i.getSeverity(), "BLOCKER")).count();
//            jiejuezuduanAll = jiejuezuduan + jiejuezuduanAll;
//
//            // 解决-严重 CRITICAL
//            long jiejueyanzhong = FIXED.stream().filter(i -> Objects.equals(i.getSeverity(), "CRITICAL")).count();
//            jiejueyanzhongAll = jiejueyanzhong + jiejueyanzhongAll;
//
//            // 解决-主要  MAJOR
//            long jiejueciyao = FIXED.stream().filter(i -> Objects.equals(i.getSeverity(), "MAJOR")).count();
//            jiejueciyaoAll = jiejueciyao + jiejueciyaoAll;
//
//            // 解决-次要  MINOR
//            long jiejeuciyao = FIXED.stream().filter(i -> Objects.equals(i.getSeverity(), "MINOR")).count();
//            jiejueciyaoAll = jiejeuciyao + jiejueciyaoAll;



            /***    删除   REMOVED   **/
            List<Issues> REMOVED = issues.stream().filter(i -> Objects.equals(i.getResolution(), "REMOVED")
            ).collect(Collectors.toList());
            REMOVED_ALL = REMOVED.size() + REMOVED_ALL;

            // 删除-阻断  BLOCKER
            long shanchuzuduan = REMOVED.stream().filter(i -> Objects.equals(i.getSeverity(), "BLOCKER")).count();
            shanchuzuduanAll = shanchuzuduan + shanchuzuduanAll;

            // 删除-严重 CRITICAL
            long shanchuyanzhong = REMOVED.stream().filter(i -> Objects.equals(i.getSeverity(), "CRITICAL")).count();
            shanchuyanzhongAll = shanchuyanzhong + shanchuyanzhongAll;

            // 删除-主要  MAJOR
            long shanchuzhuyao = REMOVED.stream().filter(i -> Objects.equals(i.getSeverity(), "MAJOR")).count();
            shanchuzhuyaoAll = shanchuzhuyao + shanchuzhuyaoAll;

            // 删除-次要  MINOR
            long shanchuciyao = REMOVED.stream().filter(i -> Objects.equals(i.getSeverity(), "MINOR")).count();
            shanchuciyaoAll = shanchuciyao + shanchuciyaoAll;

            pageNumber++;
//            System.out.println("当前页:"+pageNumber);

        }while (admin != null && !CollectionUtils.isEmpty(admin.getIssues()));

        System.out.println("不修复总数:"+WONTFIX_ALL);
        System.out.println("误判总数:"+FALSE_POSITIVE_ALL);
        System.out.println("解决:"+FIXED_ALL);



        System.out.println("开始持久化");
        analy.setBlockerCount(blockerCountAll);
        analy.setCriticalCount(CRITICALCountAll);
        analy.setMajorCount(MAJORCountAll);
        analy.setMinorCount(MINORCountAll);

        analy.setWontFixAll(WONTFIX_ALL);
        analy.setFalsePositiveAll(FALSE_POSITIVE_ALL);
        analy.setFixedAll(FIXED_ALL);
        analy.setRemoveAll(REMOVED_ALL);

        analy.setJiejuezuduanAll(jiejuezuduanAll);
        analy.setJiejueyanzhongAll(jiejueyanzhongAll);
        analy.setJiejuezhuyaoAll(jiejuezhuyaoAll);
        analy.setJiejueciyaoAll(jiejueciyaoAll);


        analy.setShanchuzuduanAll(shanchuzuduanAll);
        analy.setShanchuyanzhongAll(shanchuyanzhongAll);
        analy.setShanchuzhuyaoAll(shanchuzhuyaoAll);
        analy.setShanchuciyaoAll(shanchuciyaoAll);


        analy.setWupanzuduanAll(wupanzuduanAll);
        analy.setWupanyanzhongAll(wupanyanzhongAll);
        analy.setWupanzhuyaoAll(wupanzhuyaoAll);
        analy.setWupanciyaoAll(wupanciyaoAll);


        analyDao.save(analy);

        System.out.println(analy);
    }


    @org.junit.Test
    public void a(){
        String a = "// https://sonarqube.iflytek.com:9100/dashboard?id=QYPT-QYAPIJKFW";
        System.out.println(a.substring(0,37));
        System.out.println(a.substring(51));
    }

    @Data
    public  class T implements Runnable{
        private Analy analy;

        @Override
        public void run() {
            exec(analy);
        }
    }


}
